<template>
  <div id="main" ref="echartDom"></div>
</template>

<script setup>
import * as echarts from "echarts";
import { onMounted, onUnmounted, ref } from "vue";

// 基于准备好的dom，初始化echarts实例
const echartDom = ref(null);
let myChart = null;
onMounted(() => {
  //获取大小
  myChart = echarts.init(echartDom.value);
  // 绘制图表
  myChart.setOption({
    tooltip: {
      trigger: "item",
    },
    legend: {
      top: "5%",
      left: "center",
    },
    series: [
      {
        name: "Access From",
        type: "pie",
        radius: ["40%", "70%"],
        avoidLabelOverlap: false,
        padAngle: 5,
        itemStyle: {
          borderRadius: 10,
        },
        label: {
          show: false,
          position: "center",
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 40,
            fontWeight: "bold",
          },
        },
        labelLine: {
          show: false,
        },
        data: [
          { value: 1048, name: "Search Engine" },
          { value: 735, name: "Direct" },
          { value: 580, name: "Email" },
          { value: 484, name: "Union Ads" },
          { value: 300, name: "Video Ads" },
        ],
      },
    ],
  });
});
window.addEventListener("resize", () => {
  //页面变化重新加载
  myChart && myChart.resize();
});
onUnmounted(() => {
  myChart && myChart.dispose();
});
</script>

<style scoped lang="less">
#main {
  width: 100%;
  height: 300px;
  padding-top: 10px;
  margin: 0;
}
</style>
